package com.mf.ergate.web.controller;

import com.mf.ergate.web.common.utils.SensitiveDataEncryptUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 默认控制器
 * User: zhaoming
 * DateTime: 2017-11-06
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/security")
public class SecurityController {

    public Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取Key
     */
    @RequestMapping(value = "/getKey", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> getKey(HttpServletRequest request) {
        String key = SensitiveDataEncryptUtils.getKey(request);
        String value = redisTemplate.opsForValue().get(key);
        if(StringUtils.isBlank(value)){
            value = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
            redisTemplate.opsForValue().set(key, value, 180, TimeUnit.SECONDS);
        }
        Map<String, String> map = new HashMap<>();
        map.put("key", value);
        return map;
    }
}
